#include "LastExecuted.hpp"

#include "../Parameters.hpp"

using namespace BFT::messages;

LastExecuted::LastExecuted(uint32_t _seq, uint32_t _sender)
	: MacArrayMessage::MacArrayMessage(LAST_EXECUTED, computeSize(), _sender, BFT::Parameters::getOrderCount())
{
	seqNo = _seq;
	
	int bytes_size;
	boost::shared_ptr<char> bytes = getBytes(bytes_size);
	
	//write the sequence number
	*(uint32_t*)(bytes.get()+getOffset()) = htonl(seqNo);
}

LastExecuted::LastExecuted(boost::shared_ptr<char> _bytes, int _bytes_size)
	: MacArrayMessage::MacArrayMessage(_bytes, _bytes_size)
{

	unsigned int offset = getOffset();
	//read the sequence number
	seqNo = ntohl(*(uint32_t*)(_bytes.get()+offset));
	offset += SEQUENCE_NUMBER_SIZE;
	
	if (offset != _bytes_size - getAuthenticationSize())
	{
		fprintf(stderr,"trying to build a LastExec message from invalid bytes\n");
		exit(109);
	}
}

uint32_t LastExecuted::computeSize()
{
	return SEQUENCE_NUMBER_SIZE;
}

void LastExecuted::printInfo()
{
	fprintf(stdout, "LastExecuted information : \nseqNo: %d",seqNo);
}
